package cuiyt.juc.demo01;

/**
 * @author cyt
 * @create 2020-04-29 18:08
 */

// 买票的例子， synchronized
public class SynchronizedDemo01 {
    // 定义资源类
    public static void main(String[] args) {
        tick tick = new tick();
        // 使用 lambda 表达式来开启多线程
        new Thread(() -> {
            for (int i = 0; i < 5; i++)
                tick.sale();
        }, "A").start();


        new Thread(() -> {
            for (int i = 0; i < 5; i++)
                tick.sale();
        }, "B").start();

        new Thread(() -> {
            for (int i = 0; i < 15; i++)
                tick.sale();
        }, "C").start();
    }

}

class tick {

    private Integer number = 20;

    public synchronized void sale() {
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出了" + (number--) + "票， 剩余： " + number);
        }

    }
}
